package com.miao.teacher.app;

import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Stack;

import com.espeaker.sdk.api.MiaoApi;
import com.miao.teacher.utils.Utils;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

public class ApplicationMain extends Application {

	private static Stack<Activity> activityStack;

	private static ApplicationMain singleton;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		MiaoApi.IsDebug=false;
		singleton = this;
		JPushInterface.setDebugMode(false); // PRODUCTION
		JPushInterface.init(this);
		JPushInterface.setLatestNotificationNumber(getApplicationContext(), 1);
		JPushInterface.stopPush(getApplicationContext());
		JPushInterface.onResume(this);
		JPushInterface.resumePush(getApplicationContext());
		setTag("teacher");
	}

	private void setTag(String tag) {
		if (TextUtils.isEmpty(tag)) {
			return;
		}
		String[] sArray = tag.split(",");
		Set<String> tagSet = new LinkedHashSet<String>();
		for (String sTagItme : sArray) {
			if (!Utils.isValidTagAndAlias(sTagItme)) {
				return;
			}
			tagSet.add(sTagItme);
		}

		// save tag to sharedPreference
		SharedPreferences sharedPref = getSharedPreferences(
				TApp.PREFERENCE_FILE, Context.MODE_PRIVATE);
		Set<String> curTags = sharedPref.getStringSet(TApp.PREFERENCE_TAGS,
				null);
		if (curTags != null && curTags.containsAll(tagSet)
				&& tagSet.containsAll(curTags)) {
			return;
		}
		JPushInterface.setAliasAndTags(getApplicationContext(), null, tagSet,
				mTagsCallback);
		

	}

	private final TagAliasCallback mTagsCallback = new TagAliasCallback() {
		@Override
		public void gotResult(int code, String alias, Set<String> tags) {
			String logs;
			switch (code) {
			case 0:
				logs = "Set tag and alias success";
				break;

			case 6002:
				logs = "Failed to set alias and tags due to timeout. Try again after 60s.";
				break;

			default:
			}
		}
	};

	// Returns the application instance
	public static ApplicationMain getInstance() {
		return singleton;
	}

	/**
	 * add Activity 添加Activity到栈
	 */
	public void addActivity(Activity activity) {
		if (activityStack == null) {
			activityStack = new Stack<Activity>();
		}
		activityStack.add(activity);
	}

	/**
	 * get current Activity 获取当前Activity（栈中最后一个压入的）
	 */
	public Activity currentActivity() {
		Activity activity = activityStack.lastElement();
		return activity;
	}

	/**
	 * 结束当前Activity（栈中最后一个压入的）
	 */
	public void finishActivity() {
		Activity activity = activityStack.lastElement();
		finishActivity(activity);
	}

	/**
	 * 结束指定的Activity
	 */
	public void finishActivity(Activity activity) {
		if (activity != null) {
			activityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}

	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls) {
		for (Activity activity : activityStack) {
			if (activity.getClass().equals(cls)) {
				finishActivity(activity);
			}
		}
	}

	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity() {
		for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();
	}

	/**
	 * 退出应用程序
	 */
	public void AppExit() {
		try {
			finishAllActivity();
		} catch (Exception e) {
		}
	}

}
